/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package sql

import (
	"github.com/rs/zerolog/log"
	"github.com/spf13/cobra"
	"satellite/app/sql"
	"satellite/pkg/errorx"
	"time"
)

var Cmd = sqlCmd

// sqlCmd represents the sqlCmd command
var sqlCmd = &cobra.Command{
	Use:   "sql",
	Short: "在集群执行sql",
	Long:  ``,
	Args: func(cmd *cobra.Command, args []string) error {
		if sqlOption.Name == "" && len(args) == 0 {
			log.Info().Msg("name is required")
			return errorx.MissNameErr
		}
		if sqlOption.Name == "" {
			sqlOption.Name = args[0]
		}
		sqlOption.Fill()
		return nil
	},
	Run: func(cmd *cobra.Command, args []string) {
		runner := sql.NewSql(sqlOption)
		err := runner.Run()
		if err != nil {
			log.Warn().Err(err).Send()
			return
		}
	},
}

var (
	sqlOption = new(sql.Option)
)

func init() {
	sqlCmd.Flags().StringVarP(&sqlOption.Name, "name", "", "", "资源名称")
	sqlCmd.Flags().StringVarP(&sqlOption.Namespace, "namespace", "n", "", "资源所在命名空间")
	sqlCmd.Flags().DurationVarP(&sqlOption.Timeout, "timeout", "t", 10*time.Second, "资源超时")
	sqlCmd.Flags().IntSliceVarP(&sqlOption.Ordinal, "ordinal", "o", nil, "指定pod序列,默认所有pod")
	sqlCmd.Flags().StringVarP(&sqlOption.Cmd, "cmd", "e", "", "sql 指令")
}
